import vm from "../main.js";
const WXREQUEST = {
	async wxLogin() {
		try {
			const {
				code
			} = await wx.login();
			// return console.log('wxlogin.code>>>>>>>>>>>>>>>>>>>>>>>>>>>>',code);
			// return console.log(code)
			const {
				data
			} = await vm.$u.post(vm.$restUrl.getOpenIdByCode, {
				code
			});
			// vm.$u.vuex('wxLoginResult',data);
			// console.log(vm.wxLoginResult);
			return {
				...data,
				code
			};
		} catch (e) {
			console.log(e);
			vm.$showToast('微信预登录失败');
		}
	},
	downloadFile(url) {
		return new Promise((resolve, reject) => {
			// wx.downloadFile({
			//  url,
			//  success:(res)=>{
			// 	 console.log(res)
			// 	 resolve(res);
			//  },
			//  fail: (err) => {
			//  	reject(err);
			//  }
			// })
			uni.downloadFile({
				url,
				success: (res) => {
					resolve(res);
				},
				fail: (err) => {
					reject(err);
				}
			});
		})
	},
	shareFile(filePath, type, filename) {
		return new Promise((resolve, reject) => {
			if (type == 'video') {
				wx.shareVideoMessage({
					videoPath: filePath,
					success: (res) => {
						resolve(res);
					},
					fail: (err) => {
						reject(err);
					}
				})
			} else if (type == 'file') {
				wx.shareFileMessage({
					filePath: filePath,
					fileName: filename,
					success: (res) => {
						resolve(res);
					},
					fail: (err) => {
						reject(err);
					}
				})
			} else if (type == 'image') {
				wx.showShareImageMenu({
					path: filePath,
					success: (res) => {
						resolve(res);
					},
					fail: (err) => {
						reject(err);
					}
				})
			}
		})
	},
	collectFile(filePath, type, filename) {
		return new Promise((resolve, reject) => {
			if (type == 'video') {
				wx.addVideoToFavorites({
					videoPath: filePath,
					success: (res) => {
						resolve(res);
					},
					fail: (err) => {
						reject(err);
					}
				})
			} else if (type == 'file') {
				wx.addFileToFavorites({
					filePath: filePath,
					fileName: filename,
					success: (res) => {
						resolve(res);
					},
					fail: (err) => {
						reject(err);
					}
				})
			} else if (type == 'image') {
				wx.showShareImageMenu({
					path: filePath,
					success: (res) => {
						resolve(res);
					},
					fail: (err) => {
						reject(err);
					}
				})
			}
		})
	},
	WXPay(options) {
		return new Promise((resolve, reject) => {
			uni.requestPayment({
				provider: 'wxpay',
				timeStamp: `${options.timeStamp}`,
				package: `prepay_id=${options.package.prepay_id}`,
				signType: 'RSA',
				nonceStr: options.nonceStr,
				paySign: options.paySign,
				success: (res) => {
					console.log('微信支付调用成功>>>>>>>>>>>>>>>>>', res)
					resolve(res)
				},
				fail: (err) => {
					console.log('微信支付调用失败>>>>>>>>>>>>>>>>>', err);
					reject(err);
					vm.$u.route(`/pages/order_detail/orderDetail?taskCode=${options.task_code}`)
				}
			})
		})
	},
	//获取用户设置权限，如果没有对应权限则跳转到设置页面
	getPermission(permission,permissionName) {
		console.clear()
		return new Promise((resolve, reject) => {
			wx.getSetting({
				success: async (res) => {
					console.log(res.authSetting)
					if (Object.keys(res.authSetting).indexOf(permission) == -1) {
						wx.authorize({
							scope: permission,
							success: (res) => {
								resolve()
							},
							fail: async (err) => {
								vm.$showToast(`请打开${permissionName}使用权限`)
								try {
									await this.openSetting(permission,permissionName)
									resolve()
								} catch (e) {
									reject()
								}
							}
						})
					} else if (!res.authSetting[permission]) {
						vm.$showToast(`请打开${permissionName}使用权限`)
						try {
							await this.openSetting(permission,permissionName)
							resolve()
						} catch (e) {
							reject()
						}
					} else {
						resolve()
					}
				},
				fail: (err) => {
					vm.$showToast('获取小程序权限设置失败');
					reject();
				}
			})
		})
	},
	openSetting(permission,permissionName) {
		return new Promise((resolve, reject) => {
			wx.openSetting({
				success: (res) => {
					console.log(res.authSetting)
					console.log('success>>>>>>>>>>>>>>>>>', res)
					if (!res.authSetting[permission]) {
						vm.$showToast(`请打开${permissionName}使用权限`)
						reject();
					} else {
						resolve();
					}
				},
				fail: (err) => {
					// vm.$showToast('打开小程序权限设置列表失败');
					console.log('err>>>>>>>>>>>>>>>>>>>', err);
					reject();
				}
			})
		})

	}
}

export default WXREQUEST;
